package org.vison.wonfu.framework.soa.governance;

import io.prometheus.client.Histogram;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.vison.wonfu.framework.commons.Constants;
import org.vison.wonfu.framework.commons.tools.MetricClient;
import org.vison.wonfu.framework.soa.Invocation;

public class SoaMetric {

    private static final Histogram histogram = Histogram.build()
            .name("soa_request_duration")
            .help("Duration of SOA requests")
            .labelNames("method", "status_code","from_appid","to_appid")
            .register();

    public static void recordRequestDuration(Invocation invocation, SimpleHttpResponse response, long requestDuration) {
        String methodName = invocation.getCommandKey();
        int httpStatusCode = response.getCode();
        String toAppid = invocation.getRemoteApplication();
        String fromAppid = Constants.getAppid();
        MetricClient.histogramObserve(histogram, requestDuration, methodName, String.valueOf(httpStatusCode),fromAppid,toAppid);
    }
}
